﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YinLong.Utils.Core.Net;

namespace YinLong.Utils.Vcode.SMS
{
    public class KeJiChong
    {
        HttpHelperMin helper = new HttpHelperMin();
        HttpItemMin item = new HttpItemMin();
        HttpResultMin result = new HttpResultMin();
        private string _user;
        private string _pwd;

        /// <summary>
        /// 获取用户信息接口
        /// </summary>
        /// <param name="apiId"></param>
        /// <param name="apiSecret"></param>
        /// <returns></returns>
        public string GetUserInfo(string apiId,string apiSecret)
        {
          
            item = new HttpItemMin();
            item.URL =string.Format( "http://mapi.aiputime.com/http.action?apiType=GetUserInfo&apiId={0}&apiSecret={1}", apiId, apiSecret);
            result = helper.GetHtml(item);
            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c=="1")
            {
                _user = apiId;
                _pwd = apiSecret;
                return (string)jsonObj["score"];
            }
            else 
            {
                return c;
            }
        }
        /// <summary>
        /// 获取/占用手机号码接口
        /// </summary>
        /// <param name="pId"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool GetPhone(string pid, out string phone)
        {
            phone = "";
            item = new HttpItemMin();
            item.URL = string.Format("http://mapi.aiputime.com/http.action?apiType=GetPhone&apiId={0}&apiSecret={1}&pId={2}&pr=&cy=&cr=&h=&pn=&n=", _user, _pwd, pid);
            result = helper.GetHtml(item);
            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c == "1")
            {
                string json = result.Html;
                JObject jo = (JObject)JsonConvert.DeserializeObject(json);
                phone = jo["data"][0].ToString();

                //JObject jsonOb = JObject.Parse(result.Html);
                //phone=(jsonOb["data"][0].ToString());

                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 释放号码
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool Release(string pid,string phone)
        {

            item = new HttpItemMin();
            item.URL = string.Format("http://mapi.aiputime.com/http.action?apiType=Release&apiId={0}&apiSecret={1}&pId={2}&pn={3}", _user, _pwd,pid,phone);
            result = helper.GetHtml(item);
            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 发送短信接口
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool SendMsg(string pid, string phone,string sm,string sn)
        {

            item = new HttpItemMin();
            item.URL = string.Format("http://mapi.aiputime.com/http.action?apiType=SendMsg&apiId={0}&apiSecret={1}&pId={2}&pn={3}&sm={4}&sn={5}&dev=wangyinlon", _user, _pwd, pid, phone,sm,sn);
            result = helper.GetHtml(item);
            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 通过手机号码获取验证码接口
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="phone"></param>
        /// <param name="mc"></param>
        /// <returns></returns>
        public bool GetCode(string pid, string phone, out string mc)
        {
            mc = "";
            item = new HttpItemMin();
            item.URL = string.Format("http://mapi.aiputime.com/http.action?apiType=GetCode&apiId={0}&apiSecret={1}&pId={2}&pn={3}&dev=wangyinlon", _user, _pwd,pid, phone);
            result = helper.GetHtml(item);

            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c == "1")
            {
                string json = result.Html;
                JObject jo = (JObject)JsonConvert.DeserializeObject(json);
                mc = jo["mc"].ToString();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 手机号码加入黑名单接口
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool AddBlack(string pid, string phone)
        {
  
            item = new HttpItemMin();
            item.URL = string.Format("http://mapi.aiputime.com/http.action?apiType=AddBlack&apiId={0}&apiSecret={1}&pId={2}&pn={3}", _user, _pwd, pid, phone);
            result = helper.GetHtml(item);
            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 查询状态接口
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public bool GetStatus(string pid, string phone,out string m)
        {
            m = "";
            item = new HttpItemMin();
            item.URL = string.Format("http://mapi.aiputime.com/http.action?apiType=GetStatus&apiId={0}&apiSecret={1}&pId={2}&pn={3}", _user, _pwd, pid, phone);
            result = helper.GetHtml(item);
          
            JObject jsonObj = JObject.Parse(result.Html);
            string c = (string)jsonObj["c"];
            if (c == "1")
            {
               m = jsonObj["m"].ToString();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
